package com.yxlgateway.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.yxlcommon.common.dto.UploadDTO;
import com.yxlcommon.common.mapper.UserMapper;
import com.yxlcommon.common.pojo.UploadResponse;
import com.yxlgateway.service.UploadService;
import com.yxlgateway.utils.MinioUtil;
import com.yxlcommon.common.vo.UploadVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @author: yxl
 * @since: 2022-04-22
 **/
@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MinioUtil minioUtil;

    @Override
    public UploadVo uploadFile(MultipartFile[] files, UploadDTO uploadDTO) throws Exception {
        UploadVo uploadVo = new UploadVo();
        if (verifyUser(uploadDTO.getUsername(), uploadDTO.getPassword())) {
            for (MultipartFile file : files){
                String originalFilename = file.getOriginalFilename();
                assert originalFilename != null;
                minioUtil.uploadWorkFile(file, "stage", originalFilename);
                UploadResponse bucket01 = minioUtil.uploadWorkFile(file, "stage", originalFilename);
            }
            uploadVo.setInfo("上传文件成功");
            uploadVo.setCode(200);
        } else if (Objects.equals(null, userMapper.selectUserInformation(uploadDTO.getUsername()))) {
            uploadVo.setInfo("用户名不存在");
            uploadVo.setCode(-1);

        } else {
            uploadVo.setInfo("用户名或密码错误");
            uploadVo.setCode(-10);
        }
        return uploadVo;
    }

//    @Override
//    public RespBean<UploadVo> createBucket(UploadDTO uploadDTO) throws Exception {
//        if (verifyUser(uploadDTO.getUsername(), uploadDTO.getPassword())) {
//            minioUtil.createBucket(uploadDTO.getBucketName());
//            return RespBean.operationSuccess("创建成功", 200, );
//        } else if (Objects.equals(null, userMapper.selectUserInformation(uploadDTO.getUsername()))) {
//            return RespBean.operationFail("用户名不存在");
//        } else {
//            return RespBean.operationFail("用户名或密码错误");
//        }
//    }
//
//    @Override
//    public RespBean<UploadVo> deleteBucket(UploadDTO uploadDTO) throws Exception {
//        if (verifyUser(uploadDTO.getUsername(), uploadDTO.getPassword())) {
//            minioUtil.removeBucket(uploadDTO.getBucketName());
//            return RespBean.operationSuccess("创建成功", 200, );
//        } else if (Objects.equals(null, userMapper.selectUserInformation(uploadDTO.getUsername()))) {
//            return RespBean.operationFail("用户名不存在");
//        } else {
//            return RespBean.operationFail("用户名或密码错误");
//        }
//    }

    public boolean verifyUser(String username, String password) {
        String pwd = SaSecureUtil.md5(password);
        if (!Objects.equals(null, userMapper.findByUsernameAndPassword(username, pwd))) {
            return true;
        } else return false;
    }
}
